﻿using UnityEngine;
using System.Collections;

public class SpriteRendererAnimationComand : MonoBehaviour, IAnimationCommand {

	private SpriteRenderer renderer;

	#region IAnimationCommand implementation
	public void SetCurrentFrame (float currentTime)
	{
		if (currentTime < StartTime) {
			if (!IsStarted) {
				renderer.transform.position = TransformFromPosition;
				renderer.transform.localScale = LocalScaleFromPosition;
				renderer.color = ColorFromValue;
				this.IsStarted = true;
			}
		}
		else if (currentTime > EndTime) {
			if (!IsDone) {
				renderer.transform.position = TransformEndPosition;
				renderer.transform.localScale = LocalScaleEndPosition;
				renderer.color = ColorEndValue;
				this.IsDone = true;
			}
		}
		else {
			float time = currentTime - StartTime;
			float totalTime = EndTime - StartTime;
			float percentDone = time / totalTime;

			float currentTransX = Mathf.Lerp(TransformFromPosition.x,TransformEndPosition.x,percentDone);
			float currentTransY = Mathf.Lerp(TransformFromPosition.y,TransformEndPosition.y,percentDone);
			float currentTransZ = Mathf.Lerp(TransformFromPosition.z,TransformEndPosition.z,percentDone);
			float currentScaleX = Mathf.Lerp(LocalScaleFromPosition.x,LocalScaleEndPosition.x,percentDone);
			float currentScaleY = Mathf.Lerp(LocalScaleFromPosition.y,LocalScaleEndPosition.y,percentDone);
			float currentScaleZ = Mathf.Lerp(LocalScaleFromPosition.z,LocalScaleEndPosition.z,percentDone);
			float currentColorR = Mathf.Lerp(ColorFromValue.r,ColorEndValue.r,percentDone);
			float currentColorG = Mathf.Lerp(ColorFromValue.g,ColorEndValue.g,percentDone);
			float currentColorB = Mathf.Lerp(ColorFromValue.b,ColorEndValue.b,percentDone);
			float currentColorA = Mathf.Lerp(ColorFromValue.a,ColorEndValue.a,percentDone);

			renderer.transform.position = new Vector3(currentTransX, currentTransY, currentTransZ);
			renderer.transform.localScale = new Vector3(currentScaleX, currentScaleY, currentScaleZ);
			renderer.color = new Color(currentColorR, currentColorG, currentColorB, currentColorA);
		}
	}
	public Color ColorFromValue { get; set; }
	public Color ColorEndValue { get; set; }
	public Vector3 TransformFromPosition { get; set; }
	public Vector3 TransformEndPosition { get; set; }
	public Vector3 LocalScaleFromPosition { get; set; }
	public Vector3 LocalScaleEndPosition { get; set; }
	public float StartTime { get; set; }
	public float EndTime { get; set; }
	public Transform AnimationObject { get; set; }
	public bool IsDone { get; set; }
	public bool IsStarted { get; set; }
	#endregion

	// Use this for initialization
	void Start () {
		this.renderer = (SpriteRenderer)this.AnimationObject.gameObject.GetComponent(typeof(SpriteRenderer));
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}
